package org.cch.util;


import org.cch.bean.XMLBean;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cch on 2015/12/18.
 */
public class XMLUtil {
    public static Map<String,XMLBean> getXMLResult(String  path){
       
    	Map<String,XMLBean> map = new HashMap<String,XMLBean>();
    	/*Map mapTemp = null;
    	Map<String,XMLBean> map2 =null;*/
    	XMLBean xmlBean = null;
    	Map<String,String> resultMap =null;
    	String namespace ="";
        try{
        	SAXReader reader = new SAXReader();
        	File file = new File(path);
        	Document document = reader.read(file);	
            Element struts = document.getRootElement();
            List<Element> packages = struts.elements("package");//获得所有为package的元素
            for(Element pack:packages){//遍历package
            	List<Attribute> packageAttList = pack.attributes(); //获得元素底下的所有属性
            	String extendsType="";
            	for(Attribute attribute:packageAttList){//遍历package的属性
            		
            		if(attribute.getName().equals("namespace")){
            			//map.put(attribute.getValue(), null);
            			namespace=attribute.getValue();
            		}
            		if(attribute.getName().equals("extends")){
            			extendsType=attribute.getValue();
            		}
            	}
            	List<Element> actionList =pack.elements("action");//获得所有为action的元素
            	for(Element action:actionList){//遍历action
            		List<Attribute> actionAttributeList = action.attributes();
            		 xmlBean= new XMLBean();
            		String classType ="";
            		String method ="";
            		String actionName="";
            		for(Attribute actionAttribute :actionAttributeList){//遍历action的attribute
            			if(actionAttribute.getName().equals("name")){
            				actionName=actionAttribute.getValue();
            				
            			}
            			if(actionAttribute.getName().equals("class")){
            				classType = actionAttribute.getValue();
            				xmlBean.setClassType(classType);
            			}
            			if(actionAttribute.getName().equals("method")){
            				method = actionAttribute.getValue();
            				xmlBean.setMethod(method);
            			}
            		}
            		resultMap = new HashMap<String,String>();
            		List<Element> resultList = action.elements("result");
            		for(Element result:resultList){//遍历所有result
            			List<Attribute> resultAttributeList = result.attributes();
            			
            			for(Attribute resultAttribute:resultAttributeList){
            				if(resultAttribute.getName().equals("name")){
            					
            					if(!(result.getTextTrim().equals(""))){
            						String str =result.getText();
            						//System.out.println("name:"+resultAttribute.getValue());
            						resultMap.put(resultAttribute.getValue(),str);
            					}else{
            						resultMap.put(resultAttribute.getValue(), "");
            					}
            					
            				}
            			}
            			xmlBean.setResult(resultMap);
            			xmlBean.setParentType(extendsType);
            			map.put(namespace+"/"+actionName, xmlBean);
            		}
            		
            		
            		
            	}
            	
            	
            	
            }


        }catch (Exception e){
            e.printStackTrace();
            System.out.println("信息:配置文件解析出错!");
        }
        System.out.println("信息：struts.xml解析成功!");
        return map;
    }
}
